﻿using System;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using Telnet;
using WMI;
using System.Collections;

namespace Net_Tool
{
    public partial class frmMain : Form
    {
        private ArrayList nicList = new ArrayList();

        public frmMain()
        {
            NetworkChange.NetworkAvailabilityChanged += new
                NetworkAvailabilityChangedEventHandler(AvailabilityChangedCallback);
            InitializeComponent();
            CheckNetStatus();
            CheckSettings();
            GetNicList();
            GetDns();
        }

        private void CheckNetStatus()
        {
            lblStatus.Text = (NetworkInterface.GetIsNetworkAvailable() == true ?
                "Internet Access" : "No Connection");
            lblStatus.Image = (NetworkInterface.GetIsNetworkAvailable() == true ?
                Net_Tool.Properties.Resources.connection : 
                Net_Tool.Properties.Resources.noconnection);
        }

        private void CheckSettings()
        {
            txtIp.Text = Properties.Settings.Default.Ip;
            txtUsr.Text = Properties.Settings.Default.Username;
            txtPass.Text = Properties.Settings.Default.Password;
        }

        private void GetNicList()
        {
            nicList = Nic.GetNicList();
            cbAdapter.DataSource = nicList;
        }

        private void GetDns()
        {
            string[] dns = Nic.GetDns(cbAdapter.SelectedItem.ToString());
            if (dns.Length > 1)
            {
                rbDnsManual.Checked = true;
                gbDns.Enabled = true;
                txtDns1.Text = dns[0];
                txtDns2.Text = dns[1];
            }
            else
            {
                rbDnsAuto.Checked = true;
                gbDns.Enabled = false;
            }
        }

        private bool IsValidIp(string ip)
        {
            string[] segments = ip.Split('.');
            if (segments.Length != 4)
                throw new IpException("You have entered an invalid ip!");
            
            foreach(string segment in segments)
            {
                try
                {
                    byte value = byte.Parse(segment);
                }
                catch
                {
                    throw new IpException("You have entered an invalid ip!");
                }
            }
            return true;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            try
            {
                IsValidIp(txtIp.Text);
                TelnetClass ModemCon = new TelnetClass(txtIp.Text);
                ModemCon.Login(txtUsr.Text, txtPass.Text);
                if (ModemCon.gotPrompt)
                {
                    ModemCon.SendCmd("reboot");
                    ModemCon.Close();
                    MessageBox.Show("Modem has been resetted.", "Net Tool",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (IpException ex)
            {
                MessageBox.Show(ex.Message,
                    "Net Tool", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception)
            {
                MessageBox.Show("Could not connect to modem. Please check the ip address, username and password you have entered!",
                    "Net Tool", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void AvailabilityChangedCallback(object sender, EventArgs e)
        {
            CheckNetStatus();
        }

        private void rbDnsAuto_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rdb = (RadioButton)sender;
            if (rdb.Checked)
            {
                gbDns.Enabled = false;
                txtDns1.Text = null;
                txtDns2.Text = null;
            }
        }

        private void rbDnsManual_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rdb = (RadioButton)sender;
            if (rdb.Checked)
            {
                gbDns.Enabled = true;
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                if (rbDnsAuto.Checked)
                {
                    Nic.SetDns(cbAdapter.SelectedItem.ToString(), "", false);
                }
                else if (rbDnsManual.Checked)
                {
                    IsValidIp(txtDns1.Text);
                    IsValidIp(txtDns2.Text);
                    string dns = txtDns1.Text + ":" + txtDns2.Text;
                    Nic.SetDns(cbAdapter.SelectedItem.ToString(), dns, true);
                }

                MessageBox.Show("Dns addresses should have been changed.",
                    "Net Tool", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (IpException ex)
            {
                MessageBox.Show(ex.Message,
                    "Net Tool", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.Ip = txtIp.Text;
            Properties.Settings.Default.Username = txtUsr.Text;
            Properties.Settings.Default.Password = txtPass.Text;
            Properties.Settings.Default.Save();
        }

        private void lblLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://onatm.wordpress.com");
        }
    }
}
